﻿Imports Data

Public Class CatalogarTarea
    Private id As Integer
    Private n_tarea As String
    Private d_tarea As String
    Private estado As String



    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Try
            Me.Close()
        Catch ex As Exception
            Debug.Print(String.Format("Error en la clase: {0}, motodo:Cencelar", "Catalogar"))
        End Try
    End Sub

    Public Sub New()

        InitializeComponent()

    End Sub

    Public Sub New(ByVal id_tarea As Integer, ByVal nombre As String, ByVal descripcion As String, ByVal est As String)
        InitializeComponent()
        Me.id = id_tarea
        Me.n_tarea = nombre
        Me.d_tarea = descripcion
        Me.estado = est
    End Sub

    Private Sub CatalogarTarea_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            If (CargarDatos()) Then
                Me.CargarComboboxEstados()
            Else
                MsgBox("error")
            End If
        Catch ex As Exception

        End Try
    End Sub


    Private Function CargarDatos() As Boolean
        Try
            Me.lblTarea.Text = n_tarea
            Me.lblDescripcion.Text = d_tarea
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    Private Function CargarComboboxEstados()
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista = (From x In db.Estado _
                              Where x.id_tipo_estado = 4 _
                              Select New AbstractObject With {.ID = x.id, _
                                                              .Nombre = x.nombre}).ToList



                Dim manager As BindingManagerBase = Me.cbxEstados.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxEstados.DisplayMember = "Nombre"
                Me.cbxEstados.ValueMember = "ID"
                Me.cbxEstados.DataSource = lista
                Me.cbxEstados.Enabled = True
                manager.ResumeBinding()


                Me.cbxEstados.SelectedIndex = Me.cbxEstados.FindString(estado)
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnIngresar_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        Try
            If Me.ModificarTarea() Then
                MsgBox("Modificada correctamente", MsgBoxResult.Ok, MsgBoxStyle.Information)
                Me.Close()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function ModificarTarea() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim _nestado As Integer = cbxEstados.SelectedValue
                Dim tarea As Tareas_Pendientes = (From x In db.Tareas_Pendientes _
                                                  Where x.id = id _
                                                  Select x).FirstOrDefault

                tarea.id_estado = _nestado
                db.SubmitChanges()
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function
End Class